package ThreadSafety;

public class Exercise {
    //三个线程分别只能打印A,B,C 现在要按顺序依次打印10次 A B C
//    public static void main(String[] args) {
//        Object locked1 = new Object();
//        Object locked2 = new Object();
//        Thread t1 = new Thread(() -> {
//            System.out.print(" A ");
//
//            synchronized (locked1) {
//                locked1.notify();
//            }
//        });
//        Thread t2 = new Thread(() -> {
//            synchronized (locked1) {
//                try {
//                    locked1.wait(100);
//                } catch (InterruptedException e) {
//                    throw new RuntimeException(e);
//                }
//                System.out.print(" B ");
//            }
//            synchronized (locked2) {
//                locked2.notify();
//            }
//        });
//        Thread t3 = new Thread(() -> {
//            synchronized (locked2) {
//                try {
//                    locked2.wait();
//                } catch (InterruptedException e) {
//                    throw new RuntimeException(e);
//                }
//            }
//            System.out.println(" C ");
//        });
//        t1.start();
//        t2.start();
//        t3.start();
//    }
    public static void main(String[] args) throws InterruptedException {
        Object locker1 = new Object();
        Object locker2 = new Object();
        Object locker3 = new Object();
        Thread t1 = new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    synchronized (locker1) {
                        locker1.wait();
                    }
                    System.out.print("A");
                    synchronized (locker2) {
                        locker2.notify();
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread t2 = new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    synchronized (locker2) {
                        locker2.wait();
                    }
                    System.out.print(" B ");
                    synchronized (locker3) {
                        locker3.notify();
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread t3 = new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    synchronized (locker3) {
                        locker3.wait();
                    }
                    System.out.println("C");
                    synchronized (locker1) {
                        locker1.notify();
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        t1.start();
        t2.start();
        t3.start();

        Thread.sleep(1000);

        // 从线程 t1 启动
        synchronized (locker1) {
            locker1.notify();
        }
    }
}
